import {
  findComponentDownward,
  findComponentUpward,
  getRoute,
  getUrlParam,
  merge,
  windowClose
} from '@admin/utils/index';
import TablePlugin from '@admin/plugins/table';
import pageBanner from '../../page-banner/index.vue';
import SlotRender from '../components/SlotRender.vue';
export default {
  props: {
    pageInfo: Object,
    code: {
      type: String,
      default: ''
    },
    id: {
      type: String,
      default: ''
    }
  },
  components: {
    pageBanner,
    SlotRender
  },
  data() {
    return {
      tableInstance: null,
      treeInstance: null,
      commonInstance: null,
      btns: [] // 新增 btns
    };
  },
  beforeMount() {
    this.commonInstance = findComponentUpward(this, 'CommonPage');
  },
  created() {
    // 处理 btns 数据
    const pageBtns = this.pageInfo && Array.isArray(this.pageInfo.btns) ? this.pageInfo.btns : [];
    this.btns = pageBtns.map(btn => {
      btn.disType = btn.disType || '10';
      btn.refButton = (btn.refButton || '').split(',');
      const defaultSetting = {
        confirm: false,
        icon: {
          'icon-class': '',
          type: 'icon'
        }
      };
      btn.renderSetting = merge(defaultSetting, btn.renderSetting ? JSON.parse(btn.renderSetting) : {});
      return btn;
    });
  },
  computed: {
    tmpCode() {
      return this.pageInfo.tmp;
    },
    // btns 相关过滤全部改为用 this.btns
    toolbarBtns() {
      return this.btns.filter(btn => btn.disType === '10');
    },
    tableBtns() {
      return this.btns.filter(btn => btn.disType === '20');
    },
    tableLinkBtns() {
      return this.btns.filter(btn => btn.disType === '30');
    },
    treeBtns() {
      return this.btns.filter(btn => btn.disType === '40');
    },
    // 自定义显示位置
    customBtns() {
      return this.btns.filter(btn => btn.disType === '50');
    },
    // 子表按钮工具栏/表格中
    custFormBtns() {
      return this.btns.filter(btn => ['100', '110'].includes(btn.disType));
    },
    gridInfo() {
      return this.pageInfo.gridInfo ? JSON.parse(this.pageInfo.gridInfo) : {};
    },
    search() {
      return this.pageInfo.search;
    },
    treeInfo() {
      return this.pageInfo.treeInfo ? JSON.parse(this.pageInfo.treeInfo) : {};
    },
    tabInfo() {
      return this.pageInfo.treeInfo ? JSON.parse(this.pageInfo.treeInfo) : {};
    },
    formSetting() {
      try {
        return this.pageInfo.formSetting ? JSON.parse(this.pageInfo.formSetting) : {};
      } catch (error) {
        console.error(error);
        return {};
      }
    },
    // 父页面code，用于返回
    pCode() {
      return this.$route.query?._pcode;
    },
    showBack() {
      return Boolean(this.pCode);
    }
  },
  mounted() {
    TablePlugin._setTableInstance(this);
  },
  methods: {
    // 树点击
    emitNodeclick(emitData) {
      this.commonInstance && this.commonInstance.$emit('nodeClick', emitData);
      this.$bus.emit('node-click', emitData);
    },
    // tab点击
    emitTabclick(emitData) {
      this.commonInstance && this.commonInstance.$emit('tabClick', emitData);
      this.$bus.emit('tab-click', emitData);
    },
    getFormInfo() {
      return this.form;
    },
    getTableInstance() {
      return (this.tableInstance ||= this.$refs.lbTable);
    },
    getTreeInstance() {
      return (this.treeInstance ||= findComponentDownward(this, 'CustomTree'));
    },
    getCurrentRow() {
      this.getTableInstance();
      return new Promise((resolve, reject) => {
        if (!this.tableInstance) {
          reject('请选择行');
        } else {
          this.tableInstance
            .getCurrentRow()
            .then(data => {
              resolve(data);
            })
            .catch(msg => {
              reject(msg);
            });
        }
      });
    },
    getSelectionRows() {
      this.getTableInstance();
      return new Promise((resolve, reject) => {
        if (!this.tableInstance) {
          reject('请选择行');
        } else {
          this.tableInstance
            .getSelectionRows()
            .then(data => {
              resolve(data);
            })
            .catch(msg => {
              reject(msg);
            });
        }
      });
    },
    getSelectionRowsSync() {
      this.getTableInstance();
      return this.tableInstance.multipleSelection;
    },
    getTableDataSync() {
      return this.tableData || []
    },
    getTreeNode() {
      return new Promise((resolve, reject) => {
        const treeNode = this.currentTree;
        if (Object.keys(treeNode).length) {
          resolve(treeNode);
        } else {
          reject('请选择树节点');
        }
      });
    },
    // 获取表单数据
    getFormData() {
      if (this.tmpCode === '1140') {
        return this.$refs.complexForm.getFullFormData();
      }
      return {};
    },
    // 页面返回
    goBack() {
      const view = this.$route;
      this.$store.dispatch('tagsView/delView', view).then(({ visitedViews }) => {
        const route = getRoute(this.pCode);
        this.$router.push({
          path: route.path
        });
      });
    }
  }
};
